package com.jbp.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jbp.dao.EquipRoomDao;
import com.jbp.exception.RRException;
import com.jbp.model.Equip;
import com.jbp.model.EquipRoom;
import com.jbp.model.EquipRoomBag;
import com.jbp.model.Room;
import com.jbp.service.EquipRoomBagService;
import com.jbp.service.EquipRoomService;
import com.jbp.service.EquipService;
import com.jbp.service.RoomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.jbp.utils.PageUtils;
import com.jbp.utils.Query;

import java.util.Date;
import java.util.Map;

/**
 * 房间装备(EquipRoom)表服务实现类
 * @author cc
 * @since 2020-08-31 18:43:09
 */
@Service
public class EquipRoomServiceImpl extends ServiceImpl<EquipRoomDao, EquipRoom> implements EquipRoomService {
    @Autowired
    private EquipService equipService;
    @Autowired
    private RoomService roomService;
    @Autowired
    private EquipRoomBagService equipRoomBagService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<EquipRoom> queryWrapper = new QueryWrapper<>();
        if (params.get("id") != null){
            queryWrapper.like("id",params.get("id"));
        }
        IPage<EquipRoom> page = this.page(
                new Query<EquipRoom>().getPage(params),
                queryWrapper
        );

        return new PageUtils(page);
    }

    @Override
    public EquipRoom getByEquip(Integer roomId, Integer equipId) {
        QueryWrapper<EquipRoom> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("room_id", roomId);
        queryWrapper.eq("equip_id", equipId);
        return getOne(queryWrapper);
    }

    @Override
    public String getRoomBg(Integer roomId) {
        EquipRoom equipRoom = getOne(new QueryWrapper<EquipRoom>()
                .eq("type", "bg")
                .eq("room_id", roomId)
        );

        if (equipRoom==null) {
            return null;
        }

        Equip equip = equipService.getById(equipRoom.getEquipId());
        if (!"normal".equals(equip.getStatus())) {
            return null;
        }

        if (equip.getFree()==0) {
            EquipRoomBag equipRoomBag = equipRoomBagService.getByEquipId(roomId, equip.getId());
            if (equipRoomBag==null || equipRoomBag.getOverTime().compareTo(new Date())<0) {
                return null;
            }
        }

        return equip.getImage();
    }

    ///设置房间背景
    @Override
    public void setRoomBg(EquipRoom equipRoom, Integer userId) {
        if (equipRoom.getEquipId()==null) {
            throw new RRException("背景id不能为空");
        }
        if (equipRoom.getRoomId()==null) {
            throw new RRException("房间id不能为空");
        }
        Room room = roomService.getById(equipRoom.getRoomId());
        if (room==null || room.getUserId().intValue() != userId) {
            throw new RRException("只有房主能设置背景");
        }

        Equip equip = equipService.getById(equipRoom.getEquipId());
        if (equip==null) {
            throw new RRException("背景不存在");
        }

        //不免费
        if (equip.getFree()==0) {
            EquipRoomBag bag =  equipRoomBagService.getByEquipId(equipRoom.getRoomId(), equip.getId());
            if (bag==null) {
                throw new RRException("请先获取");
            }
        }

        EquipRoom res = getOne(new QueryWrapper<EquipRoom>()
                .eq("type", "bg")
                .eq("room_id", equipRoom.getRoomId())
        );
        if (res != null) {
            res.setEquipId(equip.getId());
            res.setUpdateTime(new Date());
            updateById(res);
        } else {
            res  = new EquipRoom();
            res.setType("bg");
            res.setRoomId(equipRoom.getRoomId());
            res.setEquipId(equip.getId());
            save(res);
        }
    }
}